package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.anno.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.enumeration.OperationType;
import com.sky.vo.DishVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

@Mapper
public interface DishMapper {

    /**
     * 根据分类id查询菜品数量
     * @param categoryId
     * @return
     */
    @Select("select count(id) from dish where category_id = #{categoryId}")
    Integer countByCategoryId(Long categoryId);

    /**
     * 菜品分页查询
     * @param queryDTO
     * @return
     */
    Page<DishVO> page(DishPageQueryDTO queryDTO);

    /**
     * 新增菜品
     * @param dish
     * @return
     */
    @AutoFill(OperationType.INSERT)
    long dish(Dish dish);

    /**
     * 新增口味
     * @param flavors
     */
    void addFlavor(List<DishFlavor> flavors);

    /**
     * 查询菜品的分类
     * @param categoryId
     * @return
     */
    @Select("select name from category where id=#{id}")
    String queryCategory(Long categoryId);

    /**
     * 根据id查询菜品信息
     * @param id
     * @return
     */
    @Select("select * from dish where id = #{id}")
    DishVO getDishByID(long id);

    /**
     * 根据id查菜品分类
     * @return
     */
    @Select("select c.name from dish d right join category c  on d.category_id = c.id where d.id =#{id}" )
     String selectCategoryId(long id);

    /**
     * 根据id查询菜品的口味
     * @param id
     * @return
     */
    @Select("select df.name,value  from dish d right join dish_flavor df on d.id = df.dish_id where d.id = #{id}")
     List<DishFlavor> selectFlavorsById(long id);
//    修改菜品信息
    @AutoFill(OperationType.UPDATE)
    void modify(Dish dish);
    /**
     * 菜品停售与启用
     * @param id
     */
    @Update("update dish set status = #{status} where id = #{id} ")
    void stopOrStart(long status, long id);

    /**
     * 批量删除菜品
     * @param id
     */
    void deleteDishes(long id);

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    List<Dish> list(Dish dish);

    /**
     * 根据条件统计菜品数量
     * @param map
     * @return
     */
    Integer countByMap(Map map);
}
